package com.shqcjd.generalplatformprojectclient.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.shqcjd.generalplatformprojectclient.R;
import com.shqcjd.generalplatformprojectclient.api.ApiService;
import com.shqcjd.generalplatformprojectclient.helper.rxjavahelper.RxObserver;
import com.shqcjd.generalplatformprojectclient.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.generalplatformprojectclient.model.ResponseData;
import com.shqcjd.generalplatformprojectclient.model.pojo.RegisterBean;
import com.shqcjd.generalplatformprojectclient.ui.base.BaseActivity;
import com.shqcjd.generalplatformprojectclient.ui.base.BasePresenter;
import com.shqcjd.generalplatformprojectclient.util.ToastUtils;
import com.shqcjd.generalplatformprojectclient.util.UIUtils;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class RegisterActivity extends BaseActivity {
    @BindView(R.id.iv_register_pic)
    ImageView ivRegisterPic;
    @BindView(R.id.et_register_phone)
    EditText etRegisterPhone;
    @BindView(R.id.btn_regist)
    Button btnRegist;
    @BindView(R.id.et_register_code)
    EditText etRegisterCode;
    @BindView(R.id.et_register_password)
    EditText etRegisterPassword;
    @BindView(R.id.cb_register)
    CheckBox cbRegister;
    @BindView(R.id.tv_register_code)
    TextView tvRegisterCode;


    private Context context;
    private String mCode="-1";

    private final int CODE_TIME = 1000;
    private int codeNum = 60;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            tvRegisterCode.setText("还剩"+codeNum+"秒");
            if(codeNum==1){
                tvRegisterCode.setTextColor(Color.BLACK);
                tvRegisterCode.setText("获取验证码");
                handler.removeCallbacks(runnable);
            }else{
                codeNum--;
                handler.postDelayed(runnable, CODE_TIME);
            }
        }
    };

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void init() {
        super.init();
        context = this;
    }

    @Override
    public void initView() {
        super.initView();
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .circleCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(this).load(R.mipmap.login_pic).apply(options).into(ivRegisterPic);

        tvRegisterCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(tvRegisterCode.getText().toString().equals("获取验证码")){
                    if(UIUtils.isMobile(etRegisterPhone.getText().toString())){
                        codeNum = 60;
                        tvRegisterCode.setTextColor(Color.GRAY);
                        getVCode(etRegisterPhone.getText().toString());
                        handler.postDelayed(runnable, CODE_TIME);
                    }else{
                        ToastUtils.showShort(context, "请输入正确的手机号");
                    }
                }

            }
        });


        cbRegister.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    etRegisterPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    etRegisterPassword.setSelection(etRegisterPassword.getText().length());
                }else{
                    etRegisterPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    etRegisterPassword.setSelection(etRegisterPassword.getText().length());
                }
            }
        });

        btnRegist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etRegisterPhone.getText().toString().trim();
                String code = etRegisterCode.getText().toString().trim();
                String password = etRegisterPassword.getText().toString().trim();
                if(UIUtils.isMobile(phone)){
                    if(code.equals(mCode)){
                        if(password.length()>=6){
                                toRegister(phone, password, code);
                        }else{
                            ToastUtils.showShort(context, "密码必须大于等于6位");
                        }
                    }else{
                        ToastUtils.showShort(context, "短信验证码错误");
                    }
                }else{
                    ToastUtils.showShort(context, "手机号格式错误");
                }
            }
        });
    }

    /**
     *    type  1甲方2乙方 默认为1
     */
    public void toRegister(String phoneNumber, String password, String verification) {
        ApiService.register(phoneNumber,password, verification, 2)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<RegisterBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("提交注册...");
                    }

                    @Override
                    public void _onNext(ResponseData<RegisterBean> registerBeanResponseData) {
                        if(registerBeanResponseData.code==0){
                            ToastUtils.showShort(context, "注册成功");
                            finish();
                        }else{
                            ToastUtils.showShort(context, registerBeanResponseData.message);
                        }
                        hideWaitingDialog();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    private void getVCode(String phoneNumber) {
        ApiService.getVCode(phoneNumber)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<String>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<String> str) {
                        mCode = str.data;
                    }

                    @Override
                    public void _onError(String errorMessage) {
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(handler!=null){
            handler.removeCallbacks(runnable);
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_register;
    }
}
